package com.zjw.zy.coreui.fragment.setting

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import com.zjw.zy.R
import com.zjw.zy.base.BaseFragment
import com.zjw.zy.bean.ImageInfo
import com.zjw.zy.constant.ImageSubPath
import com.zjw.zy.coreui.fragment.ImageContentFragment
import com.zjw.zy.coreui.view.SettingItemView
import com.zjw.zy.ext.*
import com.zjw.zy.setting.StartUpImagePathOption
import com.zjw.zy.utils.AppUtils
import com.zjw.zy.utils.PathUtils
import com.zjw.zy.utils.WindowUtils
import com.zjw.zy.viewmodel.SettingViewModel

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/10/20 10:37
 * @description：
 */
class SettingFragment : BaseFragment() {

    private val settingViewModel: SettingViewModel by activityViewModels()

    private val access: SettingItemView by lazy { findViewById(R.id.fragment_setting_access) }
    private val startUpImage: SettingItemView by lazy { findViewById(R.id.fragment_setting_start_up_image) }
    private val locationShare: SettingItemView by lazy { findViewById(R.id.fragment_setting_location_share) }
    private val wechatImage: SettingItemView by lazy { findViewById(R.id.fragment_setting_wechat_image) }
    private val loveName: SettingItemView by lazy { findViewById(R.id.fragment_setting_love_name) }
    private val selfName: SettingItemView by lazy { findViewById(R.id.fragment_setting_self_name) }

    override fun onPageEnter() {
        WindowUtils.TopBar.showBack()
        WindowUtils.TopBar.setTopBarLabelText(getString(R.string.setting_fragment_label))
        WindowUtils.TopBar.hideAllRightButton()
        WindowUtils.FootBar.hide()

        mainActivity().setFullBackGround(R.color.setting_fragment_color)
    }

    override fun onViewRestore(savedInstanceState: Bundle?) {

    }

    override fun onSubscribe() {

        settingViewModel.getLoverName().observe(this) {
            loveName.setPrevText(it?.value ?: AppUtils.getLove()!!.name)
        }

        settingViewModel.getSelfName().observe(this) {
            selfName.setPrevText(it?.value ?: AppUtils.getSelf()!!.name)
        }

        settingViewModel.getWechatBackgroundPathOption().observe(this) { option ->
            option?.let {
                PathUtils.getBitmapFromPath(it.value)?.let { bitmap ->
                    wechatImage.setImageFromBitMap(bitmap)
                }
            }
        }


        settingViewModel.getStartUpImage().observe(this) { option ->
            option?.let {
                PathUtils.getBitmapFromPath(it.value)?.let { bitmap ->
                    startUpImage.setImageFromBitMap(bitmap)
                }
            } ?: kotlin.run {

            }
        }

        settingViewModel.getLocationShare().observe(this) { option ->
            option?.value?.let {
                locationShare.makeEnable(it)
            }
        }
    }

    override fun onBindViewListener() {

        loveName.bindListener(object : SettingItemView.Listener {
            override fun onClick() {
                alertInput(title = "设置爱人的昵称", text = loveName.getPreview()) {
                    settingViewModel.setLoveName(it)
                }
            }
        })

        selfName.bindListener(object : SettingItemView.Listener {
            override fun onClick() {
                "只能由亲爱的修改哦".longToast()
            }
        })


        access.bindListener(object : SettingItemView.Listener {
            override fun onClick() {
                goWithRightAnim(R.id.nav_access_fragment)
            }
        })

        startUpImage.bindListener(object : SettingItemView.Listener {
            override fun onClick() {
                goImageContent(
                    rootPath = PathUtils.getPrivateImagePath(ImageSubPath.START_UP_IMAGE),
                    usingPath = settingViewModel.getStartUpImage().value?.value ?: "",
                    listener = object : ImageContentFragment.FuncListener {
                        override fun onPickUp(imageInfo: ImageInfo) {
                            settingViewModel.setStartUpImage(
                                StartUpImagePathOption().apply {
                                    this.value = imageInfo.path
                                })

                            "设置成功".longToast()
                        }
                    })
            }
        })

        wechatImage.bindListener(object : SettingItemView.Listener {
            override fun onClick() {
                goImageContent(
                    rootPath = PathUtils.getPrivateImagePath(ImageSubPath.WECHAT_BACKGROUND),
                    usingPath = settingViewModel.getWechatBackgroundPathOption().value?.value ?: "",
                    listener = object : ImageContentFragment.FuncListener {
                        override fun onPickUp(imageInfo: ImageInfo) {
                            settingViewModel.setWechatBackgroundImagePath(imageInfo.path)
                            "设置成功".longToast()
                        }
                    })
            }
        })

        //位置共享
        locationShare.bindListener(object : SettingItemView.Listener {
            override fun onEnable(enable: Boolean) {
                settingViewModel.setLocationShare(enable)
            }
        })
    }

    override fun onBindViewValue() {

    }

    override fun onFindView(rootView: View?) {

    }

    override fun onLayoutId(): Int {
        return R.layout.fragment_setting
    }

    override fun onPageExit() {

    }
}